define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mox/trends/info/index' + location.search,
                    add_url: 'mox/trends/info/add',
                    edit_url: 'mox/trends/info/edit',
                    del_url: 'mox/trends/info/del',
                    multi_url: 'mox/trends/info/multi',
                    import_url: 'mox/trends/info/import',
                    table: 'mox_trends_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        // { field: 'type_id', title: __('Type_id') },
                        { field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        // { field: 'content', title: __('Content'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        {
                            field: 'type',
                            title: __('标签'),
                            formatter: function (value, row, index) {
                                if (!Array.isArray(value)) return '';
                                return value.map((item, idx) => {
                                    return `<span style=" 
                                    display: inline-block;
                                    padding: 2px 6px;
                                    margin-right: 5px;
                                    background-color: ${item.color};
                                    color: #fff;
                                    border-radius: 4px;
                                    font-size: 12px;
                                    ">${item.title}</span>`;
                                }).join('');
                            }
                        },
                        {
                            field: 'msg_num', title: __('评论管理'), operate: false,
                            formatter: function (value, row, index) {
                                let num = value > 0 ? `<b style="color: #26ffb6;">（${value}）</b>` : '';
                                return `<a href="mox/trends/posts/index?t_id=${row.id}" class="btn btn-xs btn-info btn-dialog" data-area="[&quot;95%&quot;,&quot;95%&quot;]" data-shade="[0.5,&quot;#000&quot;]" title="评论管理"><i class="fa fa-list"></i> 评论管理${num}</a>`;
                            }
                        },
                        {
                            field: 'collect_num', title: __('收藏人数'), operate: false,
                            formatter: function (value, row, index) {
                                return `<a href="mox/trends/collect/index?t_id=${row.id}" class="btn btn-xs btn-info btn-dialog" data-area="[&quot;95%&quot;,&quot;95%&quot;]" data-shade="[0.5,&quot;#000&quot;]" title="收藏人数"><i class="fa fa-group"></i> ${value} 人</a>`;
                            }
                        },
                        {
                            field: 'zan_num', title: __('点赞人数'), operate: false,
                            formatter: function (value, row, index) {
                                return `<a href="mox/trends/zan/index?t_id=${row.id}" class="btn btn-xs btn-info btn-dialog" data-area="[&quot;95%&quot;,&quot;95%&quot;]" data-shade="[0.5,&quot;#000&quot;]" title="点赞人数"><i class="fa fa-group"></i> ${value} 人</a>`;
                            }
                        },

                        { field: 'num', title: __('浏览次数') },
                        { field: 'user.nickname', title: __('关联用户') },
                        { field: 'create_time', title: __('Create_time'), sortable: true, formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange' },
                        { field: 'update_time', title: __('Update_time'), sortable: true, formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange' },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table, events: Table.api.events.operate,

                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
